package com.ppx.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.ppx.pojo.User;
import com.ppx.service.UserService;
import netscape.javascript.JSObject;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.Iterator;

/**
 * @author wangxiaotao
 * @create 2021-05-20-16:06
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    @ResponseBody
    public String login(String userAccount, String userPassword){
        return JSONObject.toJSONString(userService.login(userAccount,userPassword));
    }

    //查询用户列表
    @RequestMapping("/queryAllUser")
    @ResponseBody
    public PageInfo<User> queryAllUser(int pageNum,int pageSize){
        if(pageNum==-1)
            pageNum=1;
        return userService.queryAllUser(pageNum,pageSize);
    }

    //模糊查询用户
    @RequestMapping("/queryUserByAccount")
    @ResponseBody
    public PageInfo<User> queryUserByAccount(int pageNum,int pageSize,String userAccount){
        if(pageNum==-1)
            pageNum=1;
        return userService.queryUserByAccount(pageNum,pageSize,userAccount);
    }

    //删除用户
    @RequestMapping("/delUserByUserAccount")
    @ResponseBody
    public String delUserByUserAccount(String userAccount){
        return JSONObject.toJSONString(userService.delUserById(userAccount));
    }

    //修改用户
    @RequestMapping("/updataUserByUserAccount")
    @ResponseBody
    public String updataUserByUserAccount(User user){
        return JSONObject.toJSONString(userService.updataUserByUserAccount(user));
    }

    //查询用户是否存在
    @RequestMapping("/checkUserAccount")
    @ResponseBody
    public String checkUserAccount(String userAccount){
        return JSONObject.toJSONString(userService.checkUserAccount(userAccount));
    }

    //添加用户
    @RequestMapping("/insertIntoUser")
    @ResponseBody
    public String insertIntoUser(User user){
        return JSONObject.toJSONString(userService.insertIntoUser(user));
    }

    //安全设置
    @RequestMapping("/updataSelf")
    @ResponseBody
    public User updataSelf(){
        return userService.updataSelf();
    }

    //待恢复用户
    @RequestMapping("/queryUserByState")
    @ResponseBody
    public PageInfo<User> queryUserByState(int pageNum,int pageSize){
        if(pageNum==-1)
            pageNum=1;
        return userService.queryUserByState(pageNum,pageSize);
    }

    //模糊查询状态异常用户
    @RequestMapping("/queryUserByStateByAccount")
    @ResponseBody
    public PageInfo<User> queryUserByStateByAccount(int pageNum,int pageSize,String userAccount){
        if(pageNum==-1)
            pageNum=1;
        return userService.queryUserByStateByAccount(pageNum,pageSize,userAccount);
    }

    //用户状态设置正常
    @RequestMapping("/setStateByUserAccount")
    @ResponseBody
    public String setStateByUserAccount(String userAccount){
        return JSONObject.toJSONString(userService.setStateByUserAccount(userAccount));
    }

    //用户头像上传
    @RequestMapping(value = "/updataUserImg",method = RequestMethod.POST)
    @ResponseBody
    public String updataUserImg(@RequestParam("userImg") MultipartFile file, HttpServletRequest request,String userAccount) {
        String path = request.getSession().getServletContext().getRealPath("/static/images/upload/");
        String fileFileName=file.getOriginalFilename();//获取文件名称
        String endName=fileFileName.substring(fileFileName.lastIndexOf(".") + 1);//获取文件名后缀
        String startName=fileFileName.substring(0,fileFileName.indexOf("."));//获取文件名后缀之前的名称
        if(startName.length()>10){//名称长度大于10
            startName = startName.substring(0, 9);//取前10
        }
        String tempFileName=path+startName+"."+endName;//合并文件路径和名称
        File serchfile=new File(tempFileName);//创建文件
        String randomstring=null;//产生随机字符串
        String newname=startName+"."+endName;//拼接新名
        while (serchfile.exists()){//检测新建文件名是否重名 重名就随机产生名字
            randomstring= RandomStringUtils.randomAlphanumeric(10);//产生随机字符串
            newname=randomstring+'.'+endName;//拼接新名
            tempFileName=path+newname;//合并文件路径和名称
            serchfile=new File(tempFileName);//创建文件
        }
        try {
            //创建File对象，传入目标路径参数和文件名称
            File dir = new File(path,newname);
            file.transferTo(dir);//将上传的文件复制到制定目录upload下
        }catch (Exception e) {
            e.printStackTrace();
        }
        if ("success".equals(userService.updataUserImg(newname,userAccount))){
            request.getSession().setAttribute("imgUrl",newname);
            return newname;
        }
        return  null;
    }
}
